﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RootBaseFramework;
using System.Linq.Expressions;

namespace TestRootBaseData.Service.Model
{
    public class Person : RootEntityBase<long>
    {
        //public long EntityId { get; set; }
        //public long? ParentId { get; set; }

        public string FirstName { get; set; }
        public string LastName { get; set; }

       //public Person ParentEntity { get; set; }

       readonly List<Person> _children = new List<Person>();     
        
        public IList<Person> Children
        {
            get { return _children; }
        }


        //public  bool SetRepository(RootBaseFramework.Domain.IRepository<IBloodLine, long> Repo, RootBaseFramework.Infra.Data.UnitOfWork UWork)
        //{
        //    return base.SetRepository(Repo, UWork);
        //}
       
      
        //public override void TraceBloodLine(char delimeter = '`')
        //{
          
        //    string bloodline = "0";
        //    if(Parent!=null)
        //        bloodline =Parent.BloodLine;

        //    bloodline = bloodline + delimeter + this.EntityId.ToString();

        //    BloodLine = bloodline;
        //}



       

        //public override List<object> GetMyLines(object Repo,object UWork)
        //{
            
        //    if (UWork == null)
        //    { 
        //        var context = new Service.Repository.Database.RootBaseTestContext();
        //        UWork = new RootBaseFramework.Infra.Data.UnitOfWork(context);
        //    }

        //    var uWork = (RootBaseFramework.Infra.Data.UnitOfWork)UWork;

        //    if (Repo == null)
        //    {
        //        Repo = new Service.Repository.Database.PersonRepository(uWork);
        //    }

        //    base.SetRepository(Repo, UWork);

        //   var repo = (Service.Repository.Database.PersonRepository)Repo;
          
        //    var q = repo.FindAll();
        //   var q1 = q.Where(p => p.BloodLine.StartsWith(OldBloodLine));
        //   return q1.Cast<object>().ToList();
        //}



        public override void UpdateBloodLines( object MyService )
        {
           // throw new NotImplementedException();

            var thisService = (Service.ServiceManager)MyService;

            var thisRepo = thisService.PersonRepo;

            //    var q = GetMyLines(MyRepo,MyUnitOfWork);

                 var q = thisRepo.FindAll(p => p.BloodLine.StartsWith(OldBloodLine)).ToList();

            //var qq = from p in q //thisRepo.FindAll()
            //         where SqlMethods.Like(p.BloodLine, OldBloodLine + '%') 


                 if (q.Count > 0)
                 {
                     foreach (RootEntityBase<long> item in q)
                     {
                         if (!string.IsNullOrEmpty(OldBloodLine))
                             item.BloodLine = item.BloodLine.Replace(OldBloodLine, BloodLine);
                     }

                     var Uow = thisService.UWork;
                     Uow.Commit();
                 }
        }
    }
}
